package com.atguigu.customer;

import com.atguigu.constant.MqConst;
import com.atguigu.entity.PaymentInfo;
import com.atguigu.enums.PaymentStatus;
import com.atguigu.service.PaymentInfoService;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

/**
 * @Description
 * @Author: TT
 * @Date: 2022/8/12 14:59
 */
@Component
public class PaymentCustomer {
    @Autowired
    private PaymentInfoService paymentInfoService;
    //2.交易关闭
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = MqConst.CLOSE_PAYMENT_QUEUE,durable = "false"),
            exchange = @Exchange(value = MqConst.CLOSE_PAYMENT_EXCHANGE,durable = "false",autoDelete = "true"),
            key = {MqConst.CLOSE_PAYMENT_ROUTE_KEY}
    ))
    public void updateOrderAfterPaySuccess(String outTradeNo, Message message , Channel channel) {
        System.out.println("交易关闭发送成功");
        if(!StringUtils.isEmpty(outTradeNo)) {
            PaymentInfo paymentInfo = paymentInfoService.getPaymentInfo(outTradeNo);
            if (paymentInfo != null) {
                paymentInfo.setPaymentStatus(PaymentStatus.ClOSED.name());
                paymentInfoService.updateById(paymentInfo);
            }
        }
    }
}
